package zb.WX.service.userManage;


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import zb.WX.bean.userManage.UserInfoList;
import zb.WX.bean.userManage.UserSingle;
import zb.WX.util.AccessTokenSaveUtil;
import zb.WX.util.GlobalVar;
import zb.WX.util.HttpsUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 拉取用户的基本信息
 *
 * @author Administrator
 */
public class UserInfoService {
    /*
     * 拉取单个用户的基本信息
     */
    public String getSingleUserInfo(String openId) {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();
        String urlStr = GlobalVar.GET_SINGLE_USER_INFO.replace("ACCESS_TOKEN", access_token).replace("OPENID", openId);

        String result = HttpsUtil.getHttpsToJson(urlStr, null);
        System.out.println("拉取单个用户信息：" + result);

        return result;
    }

    /*
     * 批量拉取用户的基本信息
     */
    public String getMoreUserInfo(List<UserSingle> list) {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();
        String urlStr = GlobalVar.GET_MORE_USER_INFO.replace("ACCESS_TOKEN", access_token);

        UserInfoList users = new UserInfoList();
        users.setUser_list(list);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(users);
        System.out.println("批量拉取用户信息：" + jsonStr);

        String result = HttpsUtil.getHttpsToJson(urlStr, jsonStr);
        System.out.println("批量拉取用户信息返回结果：" + result);

        return result;
    }

    public static void main(String[] args) {
        //拉取单个用户的信息
        new UserInfoService().getSingleUserInfo("oHvUE1nspMtvAfYoLQ0NjyP61dTE");


        //批量拉取用户的信息
        List<UserSingle> list = new ArrayList<UserSingle>();
        //添加需要查询的用户的openId
        UserSingle user1 = new UserSingle();
        user1.setLang("zh_CN");//简体中文
        user1.setOpenid("oHvUE1nspMtvAfYoLQ0NjyP61dTE");

        UserSingle user2 = new UserSingle();
        user2.setLang("zh_CN");//简体中文
        user2.setOpenid("oHvUE1l4ZneIiDf6BRa9SINR5XJc");

        UserSingle user3 = new UserSingle();
        user3.setLang("zh_CN");//简体中文
        user3.setOpenid("o2joUxIkZYgVNIMflxvTUX-tm6DE");

        list.add(user1);
        list.add(user2);
        /*list.add(user3);*/

        String infoResult = new UserInfoService().getMoreUserInfo(list);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(infoResult, JsonObject.class);
        JsonArray jsonArray = jsonObject.get("user_info_list").getAsJsonArray();
        //输出用户详细信息
        for (int i = 0; i < jsonArray.size(); i++) {
            String nickName = jsonArray.get(i).getAsJsonObject().get("nickname").toString();
            String sex = jsonArray.get(i).getAsJsonObject().get("sex").toString();
            String province = jsonArray.get(i).getAsJsonObject().get("province").toString();
            String city = jsonArray.get(i).getAsJsonObject().get("city").toString();
            String remark = jsonArray.get(i).getAsJsonObject().get("remark").toString();
            System.out.println("昵称:" + nickName + " || 地址：" + province + city + " || 备注名：" + remark);
        }
    }
}
